package hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import hystrix.client.ProviderClient;
import hystrix.client.ProviderGlobalClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@DefaultProperties(defaultFallback = "hystrixFunction")     //全局降级
public class HystrixCustomerController {
    @Autowired
    private ProviderClient providerClient;

    @Autowired
    private ProviderGlobalClient providerGlobalClient;

    /**
     * 降级的回调方法
     */
    public String hystrixFunction(){
        return "hystrix_customer_error";
    }

    /**
     * c1、c2会首先显示熔断的内容
     */
    @GetMapping("c1")
    public String c1(){
        return providerClient.c1();
    }

    @GetMapping("c2")
    public String c2(){
        return providerClient.c2();
    }

    /**
     * c3、c4没有设置熔断会显示降级内容
     */
    @GetMapping("c3")
    public String c3(){
        return providerClient.c3();
    }

    @GetMapping("c4")
    public String c4(){
        return providerClient.c4();
    }

    /**
     * c1、c2会首先显示熔断的内容
     */
    @GetMapping("g1")
    @HystrixCommand
    public String g1(){
        return providerGlobalClient.c1();
    }

    @GetMapping("g2")
    @HystrixCommand
    public String g2(){
        return providerGlobalClient.c2();
    }

    /**
     * c3、c4没有设置熔断会显示降级内容
     */
    @GetMapping("g3")
    @HystrixCommand(fallbackMethod = "hystrixFunction")
    public String g3(){
        return providerGlobalClient.c3();
    }

    @GetMapping("g4")
    @HystrixCommand
    public String g4(){
        return providerGlobalClient.c4();
    }
}
